package cn.ctvonline.android.kaopu.support;

import android.content.Context;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.poisearch.PoiSearch;
import com.amap.api.services.poisearch.PoiSearch.SearchBound;

/**
 * Created by dujian on 15/11/27.
 */
public class AmapSearchServer {
    private Context context;
    private int currentPage = 0;
    private int pageSize = 10;
    private PoiSearch.OnPoiSearchListener onPoiSearchListener;
    private PoiSearch.Query query;
    private PoiSearch poiSearch;

    public AmapSearchServer(Context context) {
        this.context = context;
    }

    public void initSearchParam(String keyword, String typeKeyCode, String cityCode) {
        query = new PoiSearch.Query(keyword, typeKeyCode, cityCode);
    }

    public void initSearchParam(String keyword, String typeKeyCode, String cityCode, LatLonPoint centerPoint, int radiusInMeters) {
        // keyWord表示搜索字符串，
        //第二个参数表示POI搜索类型，二者选填其一，
        //POI搜索类型共分为以下20种：汽车服务|汽车销售|
        //汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|
        //住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|
        //金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施
        //cityCode表示POI搜索区域的编码，是必须设置参数
        this.initSearchParam(keyword, typeKeyCode, cityCode);
        query.setPageSize(pageSize);// 设置每页最多返回多少条poiitem
        query.setPageNum(currentPage);//设置查询页码
        poiSearch = new PoiSearch(context, query);//初始化poiSearch对象
        SearchBound searchBound = new SearchBound(centerPoint, radiusInMeters);
        poiSearch.setBound(searchBound);
        poiSearch.setOnPoiSearchListener(onPoiSearchListener);//设置回调数据的监听器
    }

    public void poiSearch() {
        poiSearch.searchPOIAsyn();//开始搜索
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
        query.setPageNum(currentPage);//设置查询页码
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
        query.setPageSize(pageSize);
    }

    public PoiSearch.OnPoiSearchListener getOnPoiSearchListener() {
        return onPoiSearchListener;
    }

    public void setOnPoiSearchListener(PoiSearch.OnPoiSearchListener onPoiSearchListener) {
        this.onPoiSearchListener = onPoiSearchListener;
    }
}
